import unittest
from Hand import Hand
import json

class Data():
  file_path_full = 'data.json'

  def re_write_content_dict(func):
    '''
      在子函数之后，将内容进行修改。 
      装饰： 实例方法
    '''
    def inner(self, *args, **kwargs):
      result = func(self, *args, **kwargs)
      self.content_dict['positions'] = self.content_dict_position
      return result
    return inner

  def read_json(func):
    '''
      读取 json 文件内容 
      装饰： 实例方法
    '''
    def inner(self, *args, **kwargs):
      with open(self.file_path_full, 'r', encoding='utf-8') as f:
        obj = json.load(f)
        self.content_dict = obj
        self.content_dict_position = self.content_dict['positions']
        return func(self, *args, **kwargs) 
    return inner
  
  def write_json(func):
    '''
      写入 json 文件 内容 
      装饰： 实例方法
    '''
    def inner(self, *args, **kwargs):
      with open(self.file_path_full, 'w', encoding='utf-8') as f:
        result = func(self, *args, **kwargs)
        json.dump(self.content_dict, f, ensure_ascii=False)
        return result
    return inner

  def jsonStr_to_dict(str_):
    return json.loads(str_)

  def dict_to_jsonStr(dict_):
    return json.dumps(dict_)

  def __init__(self) -> None:
    self.file_path_full = self.file_path_full
    self.content_dict = None
    self.content_dict_position = None

  def __str__(self):
    return str(self.__dict__)

  @read_json
  @write_json
  @re_write_content_dict
  def save_one(self, dict1):
    self.content_dict_position.append(dict1)

  @read_json
  @write_json
  @re_write_content_dict
  def deleteOneById(self, id):
    self.content_dict_position = list(filter(lambda position: position['id'] != id, self.content_dict_position))

  @read_json
  @write_json
  @re_write_content_dict
  def deleteAll(self):
    self.content_dict_position = []

  @read_json
  def getOneById(self, id):
    return list(filter(lambda position: position['id'] == id, self.content_dict_position))

  @read_json
  def getAll(self):
    return self.content_dict_position

  @read_json
  @write_json
  @re_write_content_dict
  def updateByObj(self, newObj):
    id = newObj['id']
    def func(obj):
      if obj['id'] == id:
        return newObj
      else:
        return obj
    self.content_dict_position =  list(map(func, self.content_dict_position))

class Server():
  data = Data()

  def decorator_getAll(func):
    def inner(*args, **kwargs):
      data = Server.data.getAll()
      print(data)
      result = func('wyl350', *args, **kwargs)
      return result
    return inner

  def decorator_getAll(func):
    def inner():
      data = Server.data.getAll()
      return func(data)
    return inner

if __name__ == '__main__':
  class Test(unittest.TestCase):
    data = Data()

    @unittest.skip('init')
    def test_dict_to_json(self):
      print(
        self.data.__class__.dict_to_jsonStr([1,23,3]),
        type(self.data.__class__.dict_to_jsonStr([1,23,3])),
      )

    @unittest.skip('init')
    def test_init(self):
      print(self.data)

    @unittest.skip('read_write_json')
    def test_read_json(self):
      self.data.test(self.data)

    @unittest.skip('get_all')
    def test_get_all(self):
      print(
        self.data.getAll()
      )

    @unittest.skip('getOneById')
    def test_getOneById(self):
      print(self.data.getOneById('512fb244-de83-11ed-b66f-7032178e7348'))

    @unittest.skip('deleteOneById')      
    def test_deleteOneByid(self):
      print(self.data.deleteOneById)
      self.data.deleteOneById(1)

    @unittest.skip('saveOne')      
    def test_saveOne(self):
      self.data.save_one(Hand('白糖2309', -1, 6779, 6550).__dict__)

    @unittest.skip('deleteAll')      
    def test_deleteAll(self):
      self.data.deleteAll()

    @unittest.skip('update')      
    def test_updateOne(self):
      self.data.updateByObj(
        {
          'id': '512fb244-de83-11ed-b66f-7032178e7348', 'datetime': '2023-04-19 15:25:10.251578', 'symbol': '白糖2309', 
          'direction': -1, 
          'buy': 6779, 
          'sell': 6550, 
          'exist': True
        }
      )

  unittest.main()
